home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11270 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  60 lines

  1. Path: news.kth.se!news
  2. From: Andreas SwΣrdh <da1-asw@nada.kth.se>
  3. Newsgroups: comp.lang.c++
  4. Subject: template with unknown classes!
  5. Date: Wed, 13 Mar 1996 17:29:04 +0100
  6. Message-ID: <3146F7D0.791D@nada.kth.se>
  7. NNTP-Posting-Host: sl14.modempool.kth.se
  8. Mime-Version: 1.0
  9. Content-Type: text/plain; charset=us-ascii
  10. Content-Transfer-Encoding: 7bit
  11. X-Mailer: Mozilla 2.0 (Win95; I)
  12.  
  13. My real problem is that I havn't found any good referens about
  14. advance templates.
  15.  
  16. So my question is: If you got an exemple like the one below
  17. is there an solution to find?
  18.  
  19. class listhandler
  20. {
  21.   private:
  22.     node *head;     // my baseclass pointer. could be a Void* too, just
  23.   public:           // to make it even more complicated.
  24.     listhandler(){head = null;};
  25.     void new_object();   // here is the functions definition.  
  26.     ...
  27.     ...
  28. }
  29. // I Would like to create an listhandler that works on everything. That 
  30. can
  31. // take an unknown  class (of course it must be a defined class) and 
  32. create it
  33. // with NEW. I thought it would be easy to do with templates but haven't 
  34. had
  35. // any success yet. The problem is that I don't want the hole class to 
  36. be a
  37. // template, just THIS funktion. Is there a solution with templetes or 
  38. do 
  39. // you have to use friend in some sort of way? Maybe use of 
  40. dummy-class-pointer
  41. // just to get the right class type?! Would be most greatful for fast 
  42. replies.
  43.  
  44. // The possible function?!
  45.  
  46. template<class object>void new_object()   // or something like that!
  47. {
  48.   if(!head)
  49.     head = new object;  // where object doesn't have to be known.
  50.   ...                   // And the constuctor of object initializes it!
  51.   ...
  52. }
  53.  
  54. // It shall be intresting to see what kind of solutions you can dig out
  55. // of your closet!
  56. //
  57. // If you wanna mail me personaly: MAILTO:da1_asw@nada.kth.se
  58. //
  59. // Bye for know!!
  60.